﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Configuration;
using System.Web.Configuration;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace web.component
{
    public class email_component
    {
        public bool SendEmail(string to, string from, string display_name, string subject, string content, string host, string password, string username, int port, string bcc, string cc)
        {
            MailMessage msg = new MailMessage();
            if (!string.IsNullOrEmpty(to))
            {
                var arr_to = to.Split(',');
                foreach (var item in arr_to)
                {
                    msg.To.Add(item);
                }
            }

            msg.From = new System.Net.Mail.MailAddress(from, display_name, Encoding.UTF8);
            msg.Subject = subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = content;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = System.Net.Mail.MailPriority.High;

            //bcc
            if (!string.IsNullOrEmpty(bcc))
                msg.Bcc.Add(new MailAddress(bcc));
            //cc
            if (!string.IsNullOrEmpty(cc))
                msg.CC.Add(new MailAddress(cc));

            try
            {

                SmtpClient smtp = new SmtpClient(host, port);
                smtp.Credentials = new NetworkCredential(username, password);
                smtp.EnableSsl = true;

                smtp.Send(msg);

                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
    }

}
